<?php

namespace app\api\controller;



use app\common\model\AdminModel;
use app\common\model\member\MemberModel;
use app\common\model\RoleModel;
use wechat\Factory;

class PublicController extends Base
{

    protected $adminModel;
    /**
     * 构造方法
     */
    public function __construct()
    {
        parent::__construct();
        $this->adminModel = new AdminModel();

    }

    public function sessionkey(){
        $code    = $this->request->param('code');
        $app     = Factory::miniProgram();
        $session = $app->auth->session($code);
        /*根据openid查询会员id,有会员id生成token*/
        if(isset($session['openid'])){
            $adminInfo = $this->adminModel->where('openid', $session['openid'])->find();
            if (!empty($adminInfo)){
                $result = $this->adminModel->loginByOpenId($session['openid']);
                $result['session'] = $session;
                $this->result($result);
            } else {
                $this->result(['session'=>$session],'NO_ADMIN',0);
            }
        }else{
            $this->result(['session'=>$session],'error',0);
        }
    }
    /**
     * 用户登录
     * @throws \Exception
     */
    public function login(){
        $data = $this->request->param();
        $result = $this->adminModel->login($data['mobile'],$data['password']);
        if(!empty($data['openid'])){
            //解除原先的绑定，更新新的绑定
            $this->adminModel->where('openid',$data['openid'])->update(['openid'=>'']);
            $this->adminModel->where('id',$result['info']['id'])->update(['openid'=>$data['openid']]);
        }
        $role_nams = [];
        $customer_auth = [];
        $customer_goods_auth = [];
        $order_goods_auth = [];
        if($result['info']['role_ids']){
            $role_ids = explode(',', $result['info']['role_ids']);

            foreach($role_ids as $v){
                $roleInfo  = RoleModel::where('id',$v)->find();
                $role_nams[] = $roleInfo['name'];
                if($roleInfo['customer_auth']) $customer_auth[] = $roleInfo['customer_auth'];
                if($roleInfo['customer_goods_auth']) $customer_goods_auth[] = $roleInfo['customer_goods_auth'];
                if($roleInfo['order_goods_auth']) $order_goods_auth[] = $roleInfo['order_goods_auth'];

            }
            $customer_auth = array_unique($customer_auth);
            $customer_goods_auth = array_unique($customer_goods_auth);
            $order_goods_auth = array_unique($order_goods_auth);
        }
        $result['info']['role_name'] = implode(',',$role_nams);
//        $result['info']['customer_auth'] = $customer_auth;
//        $result['info']['customer_goods_auth'] = $customer_goods_auth;
//        $result['info']['order_goods_auth'] = $order_goods_auth;
        $this->result($result);
    }



    /**
     * 获取字典数据
     */
    public function dict(){
        $code = $this->request->param('code');
        $this->result(dict($code),'ok');
    }


    public function patch(){
        $patchSn   = $this->request->param('patch_sn');
        $patchInfo = patch($patchSn);
        if (empty($patchInfo)) exception('获取失败');
        $this->result($patchInfo,'ok');
    }

    /**
     * 批量获取碎片数据
     * @throws \Exception
     */
    public function patchs() {
        $patchSn   = $this->request->param('patch_sn');
        $patchSn   = empty($patchSn) ? [] : explode('###', $patchSn);
        $patchInfo = [];
        foreach ($patchSn as $v) {
            $info          = patch($v);
            $patchInfo[$v] = empty($info) ? '获取失败' : $info;
        }
        $this->result($patchInfo);
    }

    public function test(){
        //todo 发送订阅消息
        $data = [
            'openid' => 'oIkUd5G-AvTJd06xLXbsFLfmeWNc',     // 接收者（用户）的 openid
            'page' => '/pages/task/index',
            'msg' => [
                'thing9' => [
                    'value' => '自定义任务',
                ],
                'time3' => [
                    'value' => date('Y-m-d H:i'),
                ],
                'time7' => [
                    'value' => date('Y-m-d H:i'),
                ],
                'time8' => [
                    'value' => date('Y-m-d H:i'),
                ],
                'thing4' => [
                    'value' => '测试任务消息',
                ],
            ],
        ];
        $result = sendMiniMessage($data);
    }

}
